#include "vertex_types.fxh"
#include "Samplers.fxh"
#include "Functions.fxh"

sampler ShadowMapTextureSampler : register( s5 ) = sampler_state
{
	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	AddressU = Clamp;
	AddressV = Clamp;
};

struct SHADOW_MAP_VS
{
	float3 Position : POSITION;	
	float2 UV : TEXCOORD0; 
};

struct SHADOW_MAP_PS
{
	float4 HPosition : POSITION;
	float2 UV : TEXCOORD0; 
	float4 WorldPosition: TEXCOORD1;
};

SHADOW_MAP_PS ShadowMapVS(SHADOW_MAP_VS IN)
{
	SHADOW_MAP_PS OUT = (SHADOW_MAP_PS) 0;
	OUT.HPosition		= mul(float4(IN.Position,1.0),g_WorldViewProjectionMatrix);
	OUT.WorldPosition	= OUT.HPosition;
	OUT.UV=IN.UV;
	return OUT;
}

SHADOW_MAP_PS AnimatedModelShadowMapVS(CAL3D_HW_VERTEX_VS IN)
{
	SHADOW_MAP_PS OUT = (SHADOW_MAP_PS) 0;
	float3 l_Position=CalcAnimtedPos(float4(IN.Position.xyz,1.0), IN.Indices, IN.Weight);
	OUT.HPosition		= mul(float4(l_Position,1.0),g_WorldViewProjectionMatrix);
	OUT.WorldPosition	= OUT.HPosition;
	//OUT.UV=IN.UV;
	return OUT;
}

float4 ShadowMapPS(SHADOW_MAP_PS IN) : COLOR
{
	float4 l_DiffuseColor = tex2D(S0LinearSampler,IN.UV);
	return IN.WorldPosition.z/IN.WorldPosition.w;
}

technique StaticMeshesShadowMap
{
	pass p0
	{
		VertexShader= compile vs_3_0 ShadowMapVS();
		PixelShader= compile ps_3_0 ShadowMapPS();
	}
}

technique AnimatedModelsShadowMap
{
	pass p0
	{
		VertexShader= compile vs_3_0 AnimatedModelShadowMapVS();
		PixelShader= compile ps_3_0 ShadowMapPS();
	}
}


